﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script;
using System.Drawing;
using System.IO;
namespace ZJUploadTool
{
    public static class ExtendMethod
    {
        public static System.Web.Script.Serialization.JavaScriptSerializer
            javaScriptSerializer
        {
            get { return new System.Web.Script.Serialization.JavaScriptSerializer(); }
        }
        
        public static string To_Net_HPYS(this string s)
        {
            string strResult = "1";
            if(string.IsNullOrEmpty(s))
            {               
                switch (s)
                {
                    case "蓝":
                        strResult = "1";
                        break;
                    case "黄":
                        strResult = "2";
                        break;
                    case "黑":
                        strResult = "3";
                        break;
                    case "白":
                        strResult = "4";
                        break;
                    case "绿":
                        strResult = "5";
                        break;
                    case "黄绿":
                        strResult = "6";
                        break;
                    case "农黄":
                        strResult = "8";
                        break;
                    case "其他":
                        strResult = "9";
                        break;
                }
              
            }
            return strResult;
        }

        public static string To_Net_ZJPD(this string s, bool isDj = false)
        {
            string strResult = "N";
            if (isDj)
            {
                switch (s)
                {
                    case "0":
                        strResult = "4";
                        break;
                    case "1":
                        strResult = "0";
                        break;
                    case "2":
                        strResult = "-1";
                        break;
                    case "10":
                        strResult = "1";
                        break;
                    case "11":
                        strResult = "2";
                        break;
                }
            }
            else
            {
                switch (s)
                {
                    case "0":
                        strResult = "4";
                        break;
                    case "1":
                        strResult = "0";
                        break;
                    case "2":
                        strResult = "-1";
                        break;
                    case "3":
                        strResult = "N";
                        break;
                }
            }
            return strResult;
        }

        public static string To_Net_DetectLine(this int? i)
        {
            string _detectLine = "";
            if (i.Value == 1)
            {
                _detectLine = "A";
            }
            else if (i.Value == 2)
            {
                _detectLine = "B";
            }
            return _detectLine;
        }

        public static Image ToImage(this byte[] ImgPtr)
        {
            MemoryStream ms = new MemoryStream(ImgPtr);
            return Image.FromStream(ms);
        }

        public static byte[] ToArray(this Image source)
        {
            MemoryStream PtrSource = new MemoryStream();
            source.Save(PtrSource, System.Drawing.Imaging.ImageFormat.Jpeg);
            return PtrSource.ToArray();
        }
        /// <summary>
        /// 发送组装Json 
        /// </summary>
        /// <param name="dicRequestBody"></param>
        /// <returns></returns>
        public static string GetJsonEx(Dictionary<string, string> jsonKeyValueSet,string szIPCType)
        {
            Dictionary<string, string> dicRequestParams = new Dictionary<string, string>();
            string IPCTValue = Newtonsoft.Json.JsonConvert.SerializeObject(jsonKeyValueSet);
            dicRequestParams.Add("CompanyId", UploadConfig.GetConfig().stationID);
            dicRequestParams.Add("Source", "622901");
            dicRequestParams.Add("IPCType", szIPCType);
            dicRequestParams.Add("IPCType.value", IPCTValue);
            string jsonObject = Newtonsoft.Json.JsonConvert.SerializeObject(dicRequestParams);
            return jsonObject;
        }

        /// <summary>
        /// 从字符串组装Json
        /// </summary>
        /// <param name="szJsonObject"></param>
        /// <param name="szIPCType"></param>
        /// <returns></returns>
        public static string GetJson(string szJsonObject,string szIPCType)
        {
            Dictionary<string, string> dicRequestParams = new Dictionary<string, string>();            
            dicRequestParams.Add("CompanyId", UploadConfig.GetConfig().stationID);
            dicRequestParams.Add("Source", "622901");
            dicRequestParams.Add("IPCType", szIPCType);
            dicRequestParams.Add("IPCType.value", szJsonObject);
            string jsonObject = Newtonsoft.Json.JsonConvert.SerializeObject(dicRequestParams);
            return jsonObject;
        }

        public static Image ResizeImage(Image source, int destWidth, int destHeight)
        {
            //int sourceWidth = source.Width;
            //int sourceHeight = source.Height;
            //float nPercent = 0;
            //float nPercentW = 0;
            //float nPercentH = 0;

            //nPercentW = destWidth / sourceWidth;
            //nPercentH = destHeight / sourceHeight;

            //if (nPercentH < nPercentW)
            //    nPercent = nPercentH;
            //else
            //    nPercent = nPercentW;

            //int _width = (int)(sourceWidth * nPercent);
            //int _height = (int)(sourceWidth * nPercent);

            Bitmap bmp = new Bitmap(destWidth, destHeight);
            using (Graphics graph = Graphics.FromImage((System.Drawing.Image)bmp))
            {
                graph.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                graph.DrawImage(source, 0, 0, destWidth, destHeight);
            }
            return (System.Drawing.Image)bmp;            
        }
    }
}
